package com.swpu.eduservice.controller;


import com.swpu.commonutils.R;
import com.swpu.eduservice.Client.VodClient;
import com.swpu.eduservice.entity.EduChapter;
import com.swpu.eduservice.entity.EduVideo;
import com.swpu.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.nio.file.Path;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author ygq
 * @since 2021-10-16
 */
@RestController
@CrossOrigin
@RequestMapping("/eduservice/video")
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;

    // 注入FeignClient
    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("/addVedio")
    public R addVedio(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    //删除小节
    //TODO 后面这个方法还需要进行完善 删除小节里面的视频
    @DeleteMapping("{vedioId}")
    public R deleteVedio(@PathVariable String vedioId){

        EduVideo video = eduVideoService.getById(vedioId);
        System.out.println(video);
        String videoSourceId = video.getVideoSourceId();
        System.out.println(videoSourceId);
        //删除视频
        if(!StringUtils.isEmpty(videoSourceId))
        {
            vodClient.removeById(videoSourceId);
        }
        eduVideoService.removeById(vedioId);
        return R.ok();
    }

    //更新小节
    @PostMapping("updateVedio")
    public R updateVedio(@RequestBody EduVideo video){
        boolean flag = eduVideoService.updateById(video);
        if(flag) return R.ok();
        else return R.error();
    }

    //获取小节
    @GetMapping("getVedioInfo/{id}")
    public R getById(@PathVariable String id){
        EduVideo video = eduVideoService.getById(id);
        return R.ok().data("video",video);
    }

}

